package com.witframework.core.vaildate;

import java.util.regex.Pattern;

import org.apache.commons.validator.GenericValidator;
import org.apache.commons.validator.util.ValidatorUtils;
import org.springmodules.validation.commons.MessageUtils;


/**
 * 校验扩展
 * 
 * @author wuxinyang
 * 
 */
public class ValidationUtil implements java.io.Serializable {
	/**
	 * 检验两个输入域的值是否相同	 
	 * @return boolean
	 */
	public static boolean validateTwoFields(Object bean,
			org.apache.commons.validator.ValidatorAction va,
			org.apache.commons.validator.Field field,
			org.springframework.validation.Errors errors) {
		String value = ValidatorUtils.getValueAsString(bean, field
				.getProperty());
		String sProperty2 = field.getVarValue("secondProperty");
		String value2 = ValidatorUtils.getValueAsString(bean, sProperty2);
		if (!GenericValidator.isBlankOrNull(value)&&!GenericValidator.isBlankOrNull(value2)) {
			try {
				if (!value.equals(value2)) {
					String fieldCode = field.getKey();
					fieldCode = fieldCode.replace('(', '[').replace(')', ']');
					String errorCode = MessageUtils.getMessageKey(va, field);
			        Object[] args = MessageUtils.getArgs(va, field);
			        errors.rejectValue(fieldCode, errorCode, args, errorCode);
					return false;
				}else{
					return true;
				}

			} catch (Exception e) {				
				return false;
			}
		}
		return false;
	}
	/**
	 * 校验输入的数值是否符合设定的格式	
	 * @return boolean
	 */
	public static boolean validateNumberFormat(Object bean,
			org.apache.commons.validator.ValidatorAction va,
			org.apache.commons.validator.Field field,
			org.springframework.validation.Errors errors) {
		boolean isValid=true;
		String value = ValidatorUtils.getValueAsString(bean, field.getProperty());
		String sMaxIntegerDigits = field.getVarValue("maxIntegerDigits");//整数部分允许的最大位数
		String sMinIntegerDigits = field.getVarValue("minIntegerDigits");//整数部分允许的最小位数
		String sMaxFractionDigits = field.getVarValue("maxFractionDigits");//小数部分允许的最大位数
		String sMinFractionDigits = field.getVarValue("minFractionDigits");//小数部分允许的最小位数
		if (!GenericValidator.isBlankOrNull(value)&&!GenericValidator.isBlankOrNull(sMaxIntegerDigits)
				&&!GenericValidator.isBlankOrNull(sMaxFractionDigits)){
			if(GenericValidator.isBlankOrNull(sMinIntegerDigits)) sMinIntegerDigits="0";
			if(GenericValidator.isBlankOrNull(sMinFractionDigits)) sMinFractionDigits="0";
			isValid= Pattern.matches("^(-?)?(\\d){"+sMinIntegerDigits+","+sMaxIntegerDigits+"}((\\.)(\\d){"+sMinFractionDigits+","+sMaxFractionDigits+"})?$", value);
			if(!isValid){
				String fieldCode = field.getKey();
				fieldCode = fieldCode.replace('(', '[').replace(')', ']');
				String errorCode = MessageUtils.getMessageKey(va, field);
		        Object[] args = MessageUtils.getArgs(va, field);
		        errors.rejectValue(fieldCode, errorCode, args, errorCode);
			}
		}
		return isValid;
	}
	/**
	 * 校验输入的数值是否符合设定的格式	
	 * @return boolean
	 */
	public static boolean validateName(Object bean,
			org.apache.commons.validator.ValidatorAction va,
			org.apache.commons.validator.Field field,
			org.springframework.validation.Errors errors) {
		boolean isValid=true;		
		String value = ValidatorUtils.getValueAsString(bean, field.getProperty());
		isValid= Pattern.matches("^[\u4e00-\u9fa5[_a-zA-Z]]+$", value);
		String fieldCode = field.getKey();
		if(!isValid){
		    fieldCode = fieldCode.replace('(', '[').replace(')', ']');
			String errorCode = MessageUtils.getMessageKey(va, field);
			Object[] args = MessageUtils.getArgs(va, field);
			errors.rejectValue(fieldCode, errorCode, args, errorCode);
		}
		
		return isValid;
	}
	public static void main(String[] args) {
		String value = "1234567890.123";
		String sMaxIntegerDigits = "10";//整数部分允许的最大位数
		String sMinIntegerDigits = "0";//整数部分允许的最小位数
		String sMaxFractionDigits = "3";//小数部分允许的最大位数
		String sMinFractionDigits = "1";//小数部分允许的最小位数
		boolean isValid=Pattern.matches("^(-?)?(\\d){"+sMinIntegerDigits+","+sMaxIntegerDigits+"}((\\.)(\\d){"+sMinFractionDigits+","+sMaxFractionDigits+"})?$", value);
		System.out.println(isValid);
	}
}
